package sunstar.com.xq.presenter

import android.os.Handler
import cn.bmob.v3.BmobObject
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.QueryListListener
import cn.bmob.v3.listener.QueryListener
import cn.bmob.v3.listener.SaveListener
import sunstar.com.xq.databean.CommentData
import sunstar.com.xq.databean.CommentListData
import sunstar.com.xq.databean.UserData
import sunstar.com.xq.view.CommentView

/**
 * Created by Sun.Star on 2017/6/17.
 */
class CommentPersenter (var view:CommentView,var id:String,var list: MutableList<CommentListData>){
    private var pageIndex = 1
    fun refresh(){
        pageIndex = 1
        getData()
    }

    fun getData(){
        var query = BmobQuery<CommentData>()
        query.addWhereEqualTo("cardId",id)
        query.setLimit(20)
        if(pageIndex == 1){
            list.clear()
        }
        query.findObjects(object : FindListener<CommentData>(){
            override fun done(p0: MutableList<CommentData>?, p1: BmobException?) {
                for (i in 0..p0!!.size-1){
                   var commentdata = CommentListData()
                    commentdata.comment = p0[i]
                    BmobQuery<UserData>().getObject(p0[i].userId,object : QueryListener<UserData>(){
                        override fun done(userdata: UserData?, p1: BmobException?) {
                            commentdata.user = userdata
                            list.add(commentdata)
                            if(i == p0!!.size-1){
                                view.initData()
                            }
                        }
                    })

                }
            }
        })
    }

    fun nextData(){
        pageIndex+=1
        getData()
    }

    fun loadMore(){

    }
}